//
//  UIScrollView+HBRefresh.m
//  ZCJZ
//
//  Created by sujeking on 2019/12/23.
//  Copyright © 2019 jeking. All rights reserved.
//

#import "UIScrollView+HBRefresh.h"
#import <MJRefresh.h>

@implementation UIScrollView (HBRefresh)

- (void)addHeaderRefreshWithCallBack:(HBRefreshBlock)callBack {
    MJRefreshGifHeader *header = [MJRefreshGifHeader headerWithRefreshingBlock:^{
        callBack();
    }];
    self.mj_header = [self setHeaderRefresh:header];
}

- (void)addFooterRefreshWithCallBack:(HBRefreshBlock)callBack {
    MJRefreshBackNormalFooter *footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        callBack();
    }];
    self.mj_footer = footer;
}

- (void)beginRefreshing {
    [self.mj_header beginRefreshing];
}

- (void)endHeaderRefreshing {
    [self.mj_header endRefreshing];
}

- (void)endFooterRefreshing {
    [self.mj_footer endRefreshing];
}

- (MJRefreshGifHeader *)setHeaderRefresh:(MJRefreshGifHeader *)header{
    // 设置普通状态的动画图片
    [header setImages:[self gifArray] forState:MJRefreshStateIdle];
    // 设置一松开就会刷新的状态的动画图片
    [header setImages:[self gifArray] forState:MJRefreshStatePulling];
    // 设置正在刷新状态的动画图片
    [header setImages:[self gifArray] forState:MJRefreshStateRefreshing];
    // 设置即将刷新状态的动画图片
    [header setImages:[self gifArray] forState:MJRefreshStateWillRefresh];
    
    // 隐藏时间
    header.lastUpdatedTimeLabel.hidden = YES;
    // 隐藏状态
    header.stateLabel.hidden = YES;
    
    return header ;
}

- (NSArray*)gifArray {
    NSMutableArray *gifArr = [[NSMutableArray alloc] init];
    for(int i = 1 ; i <= 6 ; i++){
        UIImage *img = [UIImage imageNamed:[NSString stringWithFormat:@"refresh_loading%d",i]];
        if (img != nil) {
            [gifArr addObject:img];
        }
    }
    return gifArr;
}


@end
